package com.wyj.log

import android.app.Application
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.Logger
import com.elvishew.xlog.XLog
import com.elvishew.xlog.printer.ConsolePrinter
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.Printer
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.FileSizeBackupStrategy2
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.elvishew.xlog.printer.file.naming.DateFileNameGenerator
import com.wyj.utils.AppConfig
import com.wyj.utils.BuildConfig
import kotlinx.coroutines.*
import java.io.File

object LogUtil {
    @JvmStatic
    private val LOG_FOLDER_PATH = "${AppConfig.DEFAULT.APP_ROOT_DIR}/xLog/"

    @JvmStatic
    private val fileCleanStrategy = FileLastModifiedCleanStrategy(14 * 24 * 3600 * 1000L)

    @JvmStatic
    private fun getPrinters(filePrinter: FilePrinter?): Array<Printer> {
        val androidPrinter = AndroidPrinter(true)
        val consolePrinter = ConsolePrinter()
        return when (BuildConfig.DEBUG) {
            true -> {
                if (filePrinter == null) arrayOf(androidPrinter, consolePrinter) else arrayOf(androidPrinter, consolePrinter, filePrinter)
            }
            else -> {
                if (filePrinter == null) arrayOf(androidPrinter) else arrayOf(androidPrinter, filePrinter)
            }
        }
    }

    @JvmStatic
    fun createFilePrinter(): FilePrinter.Builder = FilePrinter.Builder(LOG_FOLDER_PATH).cleanStrategy(fileCleanStrategy).flattener(DefaultFlattener())

    @JvmStatic
    fun init(app: Application) {
        val filePrinter = createFilePrinter().fileNameGenerator(DateFileNameGenerator()).backupStrategy(FileSizeBackupStrategy2(1024 * 1024, 999)).build()
        val config = LogConfiguration.Builder().logLevel(LogLevel.ALL).tag("log").enableThreadInfo().enableStackTrace(2).enableBorder().build()
        XLog.init(config, *getPrinters(filePrinter))
    }

    @JvmStatic
    fun startCleanWork(viewModelScope: CoroutineScope): Job = viewModelScope.launch(Dispatchers.Default) {
        while (true) {
            delay(2 * 3600 * 1000)
            val logDir = File(LOG_FOLDER_PATH)
            if (logDir.exists()) {
                logDir.listFiles()?.forEach {
                    if (fileCleanStrategy.shouldClean(it)) {
                        try {
                            it.delete()
                        } catch (e: Exception) {
                        }
                    }
                }
            }
        }
    }

    @JvmStatic
    fun create(tag: String, filePrinter: FilePrinter?): Logger.Builder = XLog.logLevel(LogLevel.ALL).tag(tag).printers(*getPrinters(filePrinter)).enableBorder()

    @JvmStatic
    fun e(tag: String, message: String) {
        XLog.e("${tag}:${message}")
    }

    @JvmStatic
    fun d(tag: String, message: String) {
        XLog.d("${tag}:${message}")
    }

    @JvmStatic
    fun v(tag: String, message: String) {
        XLog.v("${tag}:${message}")
    }

    @JvmStatic
    fun i(tag: String, message: String) {
        XLog.i("${tag}:${message}")
    }

    @JvmStatic
    fun w(tag: String, message: String) {
        XLog.w("${tag}:${message}")
    }
}